<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <meta name="description" content="particles.js is a lightweight JavaScript library for creating particles.">
    <meta name="author" content="Vincent Garreau"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" media="screen" href="css/login/style.css">
    <link rel="stylesheet" type="text/css" href="css/login/reset.css"/>
    <link rel="stylesheet" href="./layer/theme/default/layer.css?v=3.1.1">
    <script src="js/jquery.js"></script>
    <script src="js/commit.js"></script>
    <script src="./layer/layer.js"></script>
</head>
<body>

<div id="particles-js">
    <div class="login">
        <div class="login-top">
            登录
        </div>
        <div class="login-center clearfix">
            <div class="login-center-img"><img src="images/login/name.png"/></div>
            <div class="login-center-input">
                <input type="text" name="username" value="" placeholder="请输入您的用户名" onfocus="this.placeholder=''" onblur="this.placeholder='请输入您的用户名'"/>
                <div class="login-center-input-text">用户名</div>
            </div>
        </div>
        <div class="login-center clearfix">
            <div class="login-center-img"><img src="images/login/password.png"/></div>
            <div class="login-center-input">
                <input type="password" name="password" value="" placeholder="请输入您的密码" onfocus="this.placeholder=''" onblur="this.placeholder='请输入您的密码'"/>
                <div class="login-center-input-text">密码</div>
            </div>
        </div>
        <div class="login-button">
            登陆
        </div>
    </div>
    <div class="sk-rotating-plane"></div>
</div>

<!-- scripts -->
<script src="js/login/particles.min.js"></script>
<script src="js/login/app.js"></script>
<script type="text/javascript">
    function hasClass(elem, cls) {
        cls = cls || '';
        if (cls.replace(/\s/g, '').length == 0) return false; //当cls没有参数时，返回false
        return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ');
    }

    function addClass(ele, cls) {
        if (!hasClass(ele, cls)) {
            ele.className = ele.className == '' ? cls : ele.className + ' ' + cls;
        }
    }

    function removeClass(ele, cls) {
        if (hasClass(ele, cls)) {
            var newClass = ' ' + ele.className.replace(/[\t\r\n]/g, '') + ' ';
            while (newClass.indexOf(' ' + cls + ' ') >= 0) {
                newClass = newClass.replace(' ' + cls + ' ', ' ');
            }
            ele.className = newClass.replace(/^\s+|\s+$/g, '');
        }
    }

    // document.querySelector(".login-button").onclick = function () {
    //     addClass(document.querySelector(".login"), "active")
    //     // setTimeout(function () {
    //     //     addClass(document.querySelector(".sk-rotating-plane"), "active")
    //     //     document.querySelector(".login").style.display = "none"
    //     // }, 800)
    //     // setTimeout(function () {
    //     //     removeClass(document.querySelector(".login"), "active")
    //     //     removeClass(document.querySelector(".sk-rotating-plane"), "active")
    //     //     document.querySelector(".login").style.display = "block"
    //     //     alert("登录成功")
    //     // }, 1000)
    // }

    var u = $("input[name=username]");
    var p = $("input[name=password]");
    $(".login-button").on('click', function () {
        if ($.trim(u.val()) == '' || $.trim(p.val()) == '') {
            layer.msg('用户名或密码不能为空');
            return false;
        } else {
            var reg = /^[0-9A-Za-z]+$/;
            if (!reg.exec($.trim(u.val()))) {
                layer.msg('用户名错误');
                return false;
            }
        }
        $.ajax({
            url: 'https://practice-api.bjjixin.cn/home/v1/student/login',
            type: 'POST',
            dataType: 'json',
            data: {
                phone: $.trim($("input[name=username]").val()),
                password: $.trim($("input[name=password]").val())
            }
        }).done(function (data) {
            HOME.tooken = data.access_token
            if (HOME.tooken) {
                HOME.setCookierl("HOMEistooken", true, 1, "/");
                HOME.setCookierl("HOMEcookval", HOME.tooken, 1, "/");
                HOME.setCookierl("HOMEtoken_type", data.token_type, 1, "/");

                
            }
            HOME.ajaxHOMEFn(
                {
                    url: HOME.baseUrl.loginuser,
                    type: 'GET',
                    data: {}
                },
                function (res) {
                    HOME.setCookierl("HOMEusertxt", res.name, 1, "/");
                    HOME.setCookierl("HOMEteltxt", res.phone, 1, "/");
                    console.log(HOME.getQueryVariable('page') == 'info')
                    if(HOME.getQueryVariable('page') == 'info'){
                      window.location.href=HOME.locationVal.origin+"/info.html?id="+HOME.getQueryVariable('id')+"&type="+HOME.getQueryVariable('type') + "&typeAssort=" + HOME.getQueryVariable('typeAssort') + "&curId=" + HOME.getQueryVariable('curId')                      
                    }else{
                      window.open('/index.html', '_self');
                    }
                }
            )
        }).error(function (XHR, textStatus, errorThrown) {
            let responseJSON = jQuery.parseJSON(XHR.responseText);
            if (XHR.status === 400) {
                if (responseJSON.code === 40000) {
                    //TODO 提示框
                    layer.msg(responseJSON.message);

                }
            }else if(XHR.status === 401){
                if (responseJSON.code === 40001) {
                    //TODO 提示框
                    layer.msg(responseJSON.message);
                    u.val('')
                    p.val('')
                }
            }
        });
    });



</script>
</body>
</html>